package com.example.fw;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.tests.GroupData;

public class ContactHelper extends HelperBase{

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public void initNewContactCreation() {
		 click(By.linkText("add new"));
	}

	public void fillContactForm(ContactData contact) {
		type(By.name("firstname"), contact.firstname);
		type(By.name("lastname"), contact.lastname);
		type(By.name("address"), contact.address);
		type(By.name("home"), contact.homephone);
		type(By.name("mobile"), contact.mobilephone);
		type(By.name("work"), contact.workphone);
		type(By.name("email"), contact.mailaddress);
		type(By.name("email2"), contact.mailaddress2);
//	    selectByText(By.name("bday"), contact.birthday);
//	    selectByText(By.name("bmonth"), contact.birthmonth);
//	    type(By.name("byear"), contact.birthyear);
//	    selectByText(By.name("new_group"), contact.groupname);
		type(By.name("address2"), contact.address2);
		}

	public void selectSomeContact(int rowNumber) {
	//	click(By.xpath("//input[@value="+ (index+1) + "]"));
	//	click(By.xpath("//a[@href='edit.php?id="+(index+1) +"'"+"]"));
		List <WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
		WebElement row = rows.get(rowNumber);
		List <WebElement> cells = row.findElements(By.tagName("td"));
		cells.get(6).findElement(By.tagName("a")).click();
		}
	
	//public void clickOnColumn6(int rowNumber) {
	//	selectSomeContact(rowNumber, 6);
	//}

	public void delete() {
		click(By.xpath("//input[@value='Delete']"));
		}

	public void submitContactModification() {
		click(By.xpath("//input[@value='Update']"));
	}

	public List<ContactData> getContacts() {
		List<ContactData> contacts = new ArrayList<ContactData>();
		List<WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
		for (WebElement row : rows) {
			ContactData contact = new ContactData();
			List<WebElement> cells = row.findElements(By.tagName("td"));
			String firstname=cells.get(2).getText();
			contact.firstname = firstname;
			contacts.add(contact);
		}
		return contacts;
	}
}
